package com.skt.common_hr.appmsnreq.model
{
	import com.skt.common_hr.appmsnreq.model.vo.voAppmMain;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.utilSilver.textSilver;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ReqAppmsnreqProxy extends CommonProxy
	{
		public static const NAME:String = "ReqAppmsnreqProxy";
		
		public var popList:ArrayCollection;
		public var codeList:ArrayCollection;
		
		public static var ins:ReqAppmsnreqProxy;
		public static function getInstance():ReqAppmsnreqProxy
		{
			if(ins==null)ins = new ReqAppmsnreqProxy;
			return ins;	
		}
				
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */		
		 
		public function ReqAppmsnreqProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		/**
		 * popUpList 
		 */
		public function popSaveList(sCode:String, setFunc:Function, I_RA_SEARCH:String="", I_SEARCH:String=""):void
		{
			var obj:Object = {};
			
			obj.I_PERNR 	= Variables.myPernr;
			obj.I_ORGEH		= sCode;
			obj.I_RA_SEARCH	= I_RA_SEARCH;
			obj.I_SEARCH	= I_SEARCH;
			
			obj = toJsonType(obj);
			
			loadData("Z_HR_M003", obj, popSaveListResult, listFault, setFunc);
		}
		public function popSaveListResult(event:ResultEvent):void
		{
			if(retCode==0){
				popList	= parsePopList(event.result.OTAB);
				codeList 	= event.result.T_CODE;
				event.result.func(popList, codeList);
			}
			else{
				popList = new ArrayCollection();
				event.result.func(popList);
			}
		}
		
		private function parsePopList(recArr:ArrayCollection):ArrayCollection
      	{
           var retArr:ArrayCollection = new ArrayCollection();

           for each(var obj:Object in recArr){
               
                var retObj:voAppmMain = new voAppmMain();
               
                for(var field:String in obj) {
                     if(retObj.hasOwnProperty(field)==false ) {
                     	continue;
                     }
                     if(field == "PERNR") {
						retObj[field] = textSilver.pernrFunc(obj[field]);
					}
					else {
						retObj[field] = obj[field];	
					}
                     
                }
                retArr.addItem(retObj);
           }
           return retArr;
      	}
      	private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			} 
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
	}
}